package com.offcn.service.impl;

import com.offcn.dao.ProductDao;
import com.offcn.dao.impl.ProductDaoImpl;
import com.offcn.domain.PageProduct;
import com.offcn.domain.Product;
import com.offcn.service.ProductService;

import java.util.List;

/**
 * 业务逻辑层
 */
public class ProductServiceImpl implements ProductService {
    ProductDao pd = new ProductDaoImpl();
    @Override
    public List<Product> findPros() {
        //依赖dao层,获取所有商品 返回
        return pd.findPros();
        //dao层中有查询语句
    }

    @Override
    public boolean addProduct(Product product) {
       int row = pd.insertProduct(product);

        return row>0?true:false;
    }

    @Override
    public void delProduct(String pid) {
        pd.delProduct(pid);
    }
    public void BatchdelProduct(String[] pids) {
        for (String pid : pids) {
            pd.delProduct(pid);
        }
    }

    @Override
    public Product findProsById(String pid) {
        return pd.findProById(pid);
    }

    @Override
    public PageProduct findInfoPage(int pageNumber) {
        PageProduct pp = new PageProduct();
        pp.setPageNumber(pageNumber);
        int totalCount = pd.findTotalCount();
        pp.setTotalCount(totalCount);
        int pageSize = pp.getPageSize();
        int index = (pageNumber-1)*pageSize;
        List<Product> pros = pd.findPageProduct(index,pageSize);
        pp.setList(pros);
        //begin 和 end的计算
        int totalPage = pp.getTotalPage();
        int begin = 1;
        int end = totalPage;
        if(totalPage>10){
            begin = pageNumber-5;
            end = pageNumber+4;
            if(begin<1){
                begin = 1;
                end = 10;
            }else if(end>totalPage){
                end = totalPage;
                begin = totalPage-9;
            }

        }
        pp.setBegin(begin);
        pp.setEnd(end);
        return pp;
    }
}
